package org.linlinjava.litemall.wx.service;

import cn.binarywang.wx.miniapp.bean.express.WxMaExpressPath;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.express.ExpressService;
import org.linlinjava.litemall.core.express.WxExpressService;
import org.linlinjava.litemall.core.express.dao.ExpressInfo;
import org.linlinjava.litemall.core.notify.NotifyService;
import org.linlinjava.litemall.core.notify.NotifyType;
import org.linlinjava.litemall.core.qcode.QCodeService;
import org.linlinjava.litemall.core.system.SystemConfig;
import org.linlinjava.litemall.core.task.TaskService;
import org.linlinjava.litemall.core.util.DateTimeUtil;
import org.linlinjava.litemall.core.util.IpUtil;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.linlinjava.litemall.db.util.*;
import org.linlinjava.litemall.wx.dto.BonusInfo;
import org.linlinjava.litemall.wx.dto.DiscountInfo;
import org.linlinjava.litemall.wx.task.OrderUnpaidTask;
import org.linlinjava.litemall.wx.util.IDHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.linlinjava.litemall.wx.util.WxResponseCode.*;

/**
 * 分销服务
 */
@Service
public class WxTeamService {
    private final Log logger = LogFactory.getLog(WxTeamService.class);

    @Autowired
    private LitemallUserService userService;

    @Autowired
    private WxBenefitService wxBenefitService;

    ThreadLocal<Integer> steps = new ThreadLocal<>();

    private String[] profits = SystemConfig.getTeamProfit().split(",");

    /**
     * 分销获益
     *
     * @param userId
     * @param amount
     * @return
     */
    @Async
    public void benefit(Integer userId, BigDecimal amount, String target) {
        LitemallUser user = userService.findById(userId);
        Integer pid = user.getPid();
        steps.set(0);
        doBenefit(pid, userId, amount, target);
    }


    /**
     * 递归分销获益
     *
     * @param userId
     * @param amount
     * @return
     */
    public void doBenefit(Integer userId, Integer beginId, BigDecimal amount, String target) {
        int step = steps.get();
        System.out.println("step:" + step);
        LitemallUser user = userService.findById(userId);
        BigDecimal benefit = amount.multiply(new BigDecimal(profits[step])).divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        if (benefit.compareTo(BigDecimal.ZERO) == 1) {
            wxBenefitService.team(userId, beginId, target, benefit, amount);
        }
        Integer pid = user.getPid();
        if (step >= (SystemConfig.getTeamStep() - 2)) {
            steps.remove(); // 防内存泄露
            return;
        }
        steps.set(++step);
        if (pid != null) {
            doBenefit(pid, beginId, amount, target);
        }
    }
}